<?php

namespace app\models\Child;

use Yii;

/**
 * This is the model class for table "account_user_info".
 *
 * @property integer $account_id
 * @property integer $card_id
 * @property integer $cashier_id
 * @property integer $user_id
 * @property string $account_cost
 * @property integer $card_type
 * @property integer $subclinic_id
 * @property string $desc
 * @property integer $account_type
 * @property string $create_time
 *
 * @property SysAccountCardInfo $card
 * @property TUserInfo $user
 */
class AccountUserInfo extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'account_user_info';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['card_id', 'cashier_id', 'user_id', 'card_type', 'subclinic_id', 'account_type'], 'integer'],
            [['account_cost'], 'number'],
            [['create_time'], 'safe'],
            [['desc'], 'string', 'max' => 500],
            [['card_id'], 'exist', 'skipOnError' => true, 'targetClass' => SysAccountCardInfo::className(), 'targetAttribute' => ['card_id' => 'card_id']],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => TUserInfo::className(), 'targetAttribute' => ['user_id' => 'user_id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'account_id' => 'Account ID',
            'card_id' => 'Card ID',
            'cashier_id' => 'Cashier ID',
            'user_id' => 'User ID',
            'account_cost' => 'Account Cost',
            'card_type' => 'Card Type',
            'subclinic_id' => 'Subclinic ID',
            'desc' => 'Desc',
            'account_type' => 'Account Type',
            'create_time' => 'Create Time',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCard()
    {
        return $this->hasOne(SysAccountCardInfo::className(), ['card_id' => 'card_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(TUserInfo::className(), ['user_id' => 'user_id']);
    }
    
    /*
     * 用户计次卡消费记录
     */
    public function getList($user_id)
    {
        $list = $this->find()
                ->select('sys_account_card_info.card_name,account_user_info.account_cost,account_user_info.create_time,account_type')
                ->join('left join','sys_account_card_info','sys_account_card_info.card_id = account_user_info.card_id')
                ->where(['account_user_info.user_id' => $user_id,'account_user_info.card_type' => 1])
                ->asArray()
                ->all();
        return $list;
    }
}
